package com.uwo.house.controller;

import com.uwo.house.beans.request.HomePageReq;
import com.uwo.house.beans.response.*;
import com.uwo.house.config.JWTAuthenticationToken;
import com.uwo.house.kits.CacheKits;
import com.uwo.house.service.ICustomService;
import com.uwo.house.util.UtilReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 客户端Controller
 * Created by cyn1c on 2017/7/23.
 */
@Controller
@RequestMapping("/custom")
public class CustomController {
    @Autowired
    ICustomService customService;

    /**
     * 客户端根据装修风格获取套餐列表
     *
     * @param decorationStyle
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getPackagesByStyle(@RequestParam(value = "decorationStyle",required = false) String decorationStyle) {
        List<CustomPackageResp> list = this.customService.getPackagesByStyle(decorationStyle);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 用户施工纪念册
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/schedule", method = RequestMethod.GET)
    @ResponseBody
    public Map getConstructionSchedule(@RequestHeader(value = "Authorization", required = false) String token) {
        String[] strs = token.split(" ");
        token = (strs.length == 2) ? strs[1] : null;
        JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) CacheKits.get(token);
        String userId = jwtToken.getUser().getId();
        List<CustomScheduleResp> list = this.customService.getConstructionSchedule(userId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 用户中心-我的预约
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/order", method = RequestMethod.GET)
    @ResponseBody
    public Map getUserOrder(@RequestHeader(value = "Authorization") String token) {
        String[] strs = token.split(" ");
        token = (strs.length == 2) ? strs[1] : null;
        JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) CacheKits.get(token);
        String userId = jwtToken.getUser().getId();
        List<UserOrderResp> list = this.customService.getUserOrder(userId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 首页轮播图片
     *
     * @return
     */
    @RequestMapping(value = "/pages", method = RequestMethod.GET)
    @ResponseBody
    public Map getHomePages() {
        List<HomePageResp> list = this.customService.getHomePages();
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 修改轮播图片
     *
     * @return
     */
    @RequestMapping(value = "/pages/modify", method = RequestMethod.PUT)
    @ResponseBody
    public Map deleteHomePages(@RequestBody HomePageReq homePageReq) {
        int result = this.customService.modifyHomePages(homePageReq);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "修改成功");
        }
        return UtilReturn.resErrorMap("修改失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 添加轮播图片
     *
     * @return
     */
    @RequestMapping(value = "/pages/add", method = RequestMethod.POST)
    @ResponseBody
    public Map addHomePages(@RequestBody HomePageReq homePageReq) {
        int result = this.customService.addHomePages(homePageReq);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "添加成功");
        }
        return UtilReturn.resErrorMap("添加失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 删除轮播图片
     *
     * @return
     */
    @RequestMapping(value = "/pages/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deleteHomePages() {
        int result = this.customService.deleteHomePages();
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功");
        }
        return UtilReturn.resErrorMap("删除失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 获取轮播图片详情
     *
     * @return
     */
    @RequestMapping(value = "/pages/detail", method = RequestMethod.GET)
    @ResponseBody
    public Map getHomePagesDetail(@RequestParam("id") int id) {
        HomePageResp homePageResp = this.customService.getHomePagesDetail(id);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, homePageResp);
    }


    /**
     * 获取合同图片
     * @return
     */
    @RequestMapping(value = "/contract", method = RequestMethod.GET)
    @ResponseBody
    public Map getContractPic() {
        List<ContractPicResp> list = this.customService.getContractPic();
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 添加合同图片
     *
     * @return
     */
    @RequestMapping(value = "/contract/add", method = RequestMethod.POST)
    @ResponseBody
    public Map addContractPic(@RequestParam("contractPic") String contractPic) {
        int result = this.customService.addContractPic(contractPic);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "操作成功");
        }
        return UtilReturn.resErrorMap("操作失败！", UtilReturn.ERROR_CODE);
    }

}
